﻿using System.Threading.Tasks;
using Xproject.Framework.Core;
using Xproject.Framework.Core.DataContext;
using Xproject.Framework.Core.Repositories;
using Model.Admin;
using Repository.WXProjectAdmin;
using Xproject.Util;
using Model.WXshopBuiness;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Services.WXProjectAdmin
{
   public class SystemRoleServices:Service
    {
        IRepositoryAsync<AdminRole> m_AdminRoleRepository;
        IRepositoryAsync<AdminUserRole> m_AdminUserRoleRepository;

        public SystemRoleServices(IDataContextAsync dataContext) : base(dataContext)
        {
            m_AdminRoleRepository = m_UnitOfWork.RepositoryAsync<AdminRole>();
            m_AdminUserRoleRepository = m_UnitOfWork.RepositoryAsync<AdminUserRole>();
        }
        public bool AddRole(AdminRole role, string userId)
        {
            if (role != null)
            {
                role.ID = Guid.NewGuid().ToString();
                role.EntityState = EntityState.Added;
                role.CreateTime = DateTime.Now;
                role.UpdateTime = DateTime.Now;
                role.Creator = role.Updator = userId.ToString();
                m_AdminRoleRepository.Insert(role);
                return m_UnitOfWork.SaveChanges() == 1;
            }
            return false;
        }
        /// <summary>
        /// 删除最好不做硬删除  做软删除
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public bool DelRole(AdminRole role)
        {
            if (role != null)
            {
                role.EntityState = EntityState.Deleted;
                m_AdminRoleRepository.Delete(role);
                return m_UnitOfWork.SaveChanges() == 1;
            }
            return false;
        }
        public bool EditRole(AdminRole role, string userId)
        {
            if (role != null)
            {
                role.EntityState = EntityState.Modified;
                role.UpdateTime = DateTime.Now;
                role.Updator = userId.ToString();
                m_AdminRoleRepository.Update(role);
                return m_UnitOfWork.SaveChanges() == 1;
            }
            return false;
        }

        /// <summary>
        /// 通过roleId得到
        /// </summary>
        /// <param name="menuId"></param>
        /// <returns></returns>
        public AdminRole GetAdminRoleByRoleId(int roleId)
        {
            return m_AdminRoleRepository.GetByRoleId(roleId);
        }

        public List<AdminRole> GetAdminRoleList(string site)
        {
            if (Cache_SystemInfo.AdminRoles == null)
            {
                List<AdminRole> temp = m_AdminRoleRepository.GetAllRoleList(site, null, null);
                if (temp != null && temp.Count > 0)
                {
                    Cache_SystemInfo.AdminRoles = temp;
                }
            }
            return Cache_SystemInfo.AdminRoles;
        }

        /// <summary>
        /// 得到某个站点平台的角色信息
        /// </summary>
        /// <param name="site"></param>
        /// <returns></returns>
        public List<AdminRole> GetAdminRoleList(string site, int offset, int pageSize, string roleName, string roleDesc, out int total)
        {
            total = 0;
            if (Cache_SystemInfo.AdminRoles == null)
            {
                List<AdminRole> temp = m_AdminRoleRepository.GetAllRoleList(site, null, null);
                if (temp != null && temp.Count > 0)
                {
                    Cache_SystemInfo.AdminRoles = temp;
                }
            }

            if (Cache_SystemInfo.AdminRoles != null && Cache_SystemInfo.AdminRoles.Count > 0)
            {

                List<AdminRole> temp = Cache_SystemInfo.AdminRoles;
                if (!string.IsNullOrEmpty(roleName))
                {
                    temp = Cache_SystemInfo.AdminRoles.Where(i => i.RoleName.Contains(roleName)).ToList();
                }
                if (!string.IsNullOrEmpty(roleDesc))
                {
                    temp = Cache_SystemInfo.AdminRoles.Where(i => i.RoleDesc.Contains(roleDesc)).ToList();
                }
                total = temp.Count;
                return temp.Skip(offset).Take(pageSize).ToList();

            }
            return Cache_SystemInfo.AdminRoles;
        }

        /// <summary>
        /// 得到用户的所有的用户角色
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<AdminUserRole> GetUserRoles(int userId)
        {
            return m_AdminUserRoleRepository.GetUserRoles(userId);
        }

        private bool DelUserRoles(int userId)
        {
            List < AdminUserRole > allroles = m_AdminUserRoleRepository.GetUserRoles(userId);
            m_AdminUserRoleRepository.DeleteRange(allroles);
            return  m_UnitOfWork.SaveChanges()==allroles.Count;            
        }

        public bool UpdateUserRoles(int userId, List<AdminUserRole> newRoles)
        {
            List<AdminUserRole> allroles = m_AdminUserRoleRepository.GetUserRoles(userId);
            foreach (AdminUserRole aur in allroles)
            {
                aur.EntityState = EntityState.Deleted;
            }
            foreach (AdminUserRole aur in newRoles)
            {
                aur.EntityState = EntityState.Added;
            }
            m_UnitOfWork.BeginTransaction();
            m_AdminUserRoleRepository.DeleteRange(allroles);
            m_AdminUserRoleRepository.UpdateRange(newRoles);
            int result = m_UnitOfWork.SaveChanges();
            if ( result== newRoles.Count + allroles.Count)
            {
                m_UnitOfWork.Commit();
                return true;
            }
            else {
                m_UnitOfWork.Rollback();
                return false;
            }

            
        }
    }
}
